// 模块声明
mod auth;
mod models;
mod handlers;
mod sip;
mod utils;
mod error;

// 重新导出常用类型
pub use error::{AppError, AppResult};
pub use models::*;

use tauri::Manager;
use log::info;

// 应用状态
#[derive(Default, Clone)]
pub struct AppState {
    pub current_user: Option<User>,
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // 初始化日志
    env_logger::init();
    info!("启动 Tauri SIP 管理系统");

    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .manage(AppState::default())
        .invoke_handler(tauri::generate_handler![
            // 认证相关
            handlers::auth::login,
            handlers::auth::logout,
            handlers::auth::get_current_user,
            
            // 用户管理
            handlers::user::get_user_list,
            handlers::user::create_user,
            handlers::user::update_user,
            handlers::user::delete_user,
            handlers::user::update_user_status,
            
            // 角色管理
            handlers::role::get_role_list,
            handlers::role::create_role,
            handlers::role::update_role,
            handlers::role::delete_role,
            
            // 菜单管理
            handlers::menu::get_menu_list,
            handlers::menu::create_menu,
            handlers::menu::update_menu,
            handlers::menu::delete_menu,
            
            // SIP 通话管理
            handlers::sip::get_call_list,
            handlers::sip::make_call,
            handlers::sip::hangup_call,
            handlers::sip::get_call_detail,
            
            // SIP 注册管理
            handlers::sip::get_register_list,
            handlers::sip::register_sip,
            handlers::sip::unregister_sip,
            
            // 系统监控
            handlers::monitor::get_online_users,
            handlers::monitor::get_job_list,
            handlers::monitor::get_system_stats,
            
            // 仪表盘
            handlers::dashboard::get_dashboard_data,
        ])
        .setup(|app| {
            info!("应用初始化完成");
            
            // 初始化默认用户数据
            crate::auth::AuthService::init_default_users();
            
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
